
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.List;

/**
 *
 * @author Klaas Dons
 */
public class GetHighScores implements Runnable {
    private List list;
    private Main parent;
    private String strResults = "Geen gegevens";
    private String[] arrResult;
    
    public GetHighScores(List list, Main parent){
        this.list = list;
        this.parent = parent;
    }
 
    public List getHighScoresList(){
        return this.list;
    }
    
    /**
     * Geeft de top5 highscores terug die afgehaald worden van een website.
     */
    public void run(){
        Vector result = new Vector();
        InputStream inputstream = null;
        
        HttpConnection hc = null;
        InputStream is = null;
        
        try {
            hc = (HttpConnection)Connector.open("http://www.klaasdons.nl/j2me/highScores.php", Connector.READ_WRITE);
            System.out.println("Status Line Code: " + hc.getResponseCode());
            System.out.println("Status Line Message: " + hc.getResponseMessage());
            System.out.println("Header Field Date: " + hc.getHeaderField("date"));

            inputstream = hc.openInputStream();
            int length = (int) hc.getLength();
            if (length != -1) {
              byte incomingData[] = new byte[length];
              inputstream.read(incomingData);
              this.strResults = new String(incomingData);
              System.out.println(this.strResults);
            } else  {
                ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
                int ch;
                while ((ch = inputstream.read()) != -1) {
                  bytestream.write(ch);
                }
                this.strResults = new String(bytestream.toByteArray());
                bytestream.close();
            }
        } catch (IOException e) {
            result.addElement(e.getMessage());
        } finally {
            if (is != null){
                try {
                    is.close();
                } catch (IOException e){    
                }
            }
            if (hc != null){
                try {
                    hc.close();
                } catch (IOException e){
                }
            }
        }
        System.out.println(strResults);
        arrResult = split(strResults);
        System.out.println(arrResult.length);
        this.list = new List("Highscores", List.IMPLICIT, arrResult, null);
        parent.showHighScore(this.list);
    }
    
    private String[] split(String original) {
        Vector nodes = new Vector();
        String separator = ";";

        // Parse nodes into vector
        int index = original.indexOf(separator);
        while(index>=0) {
            nodes.addElement( original.substring(0, index) );
            original = original.substring(index+separator.length());
            index = original.indexOf(separator);
        }
        
        nodes.addElement( original );

       
        String[] result = new String[ nodes.size() ];
        if( nodes.size()>0 ) {
            for(int loop=0; loop<nodes.size(); loop++) {
                result[loop] = (String)nodes.elementAt(loop);
            }
        }

        return result;
    }
}
